package com.bkav.portal.portlet.product;

import java.util.List;


import javax.mail.internet.InternetAddress;

import com.bkav.portal.portlet.product.model.Order_;
import com.bkav.portal.portlet.product.service.Order_LocalServiceUtil;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;

public class SendMailScheduler implements MessageListener {

	public void receive(com.liferay.portal.kernel.messaging.Message message)
			throws MessageListenerException {
		try {
			doReceive(message);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			_log.error("Unable to process message " + message, e);
		}
	}

	protected void doReceive(com.liferay.portal.kernel.messaging.Message message)
			throws Exception {
		try {
			List<Order_> orderList = Order_LocalServiceUtil.getOrder_s(0, 10);
			InternetAddress[] allEmailAddress = new InternetAddress[orderList.size()];
			for(int i = 0 ; i < orderList.size() ; i++){
			   allEmailAddress[i] = new InternetAddress(orderList.get(i).getEmail());
			}
			
			MailMessage mailMessage = new MailMessage();
            mailMessage.setBody("Hi,ban");
            mailMessage.setSubject("Tieu de");
            mailMessage.setFrom(new InternetAddress("smtp.bkav.com.vn"));
            mailMessage.setTo(allEmailAddress);
            MailServiceUtil.sendEmail(mailMessage);
            System.out.print("Done");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static Log _log = LogFactoryUtil.getLog(SendMailScheduler.class);

}
